<template>
  <!-- 放置一个图标 -->
  <div class="screenWrapper">
    <!--
      'exit-fullscreen'  'fullscreen'
     -->
    <!--
       问题：点击浏览器的叉号 或者esc 此时视图显示是退出了 但是图标并没有发生变化
       原因：控制icon显示的数据状态isFullScreen 并不会随着esc或者叉号的点击发生变化
       解决：根据esc或者叉号的点击 把这个状态同步到isFullScreen
       关键：如何得知esc或者叉号被点击了？？？
      -->
    <svg-icon :icon-class="isFullScreen ? 'exit-fullscreen': 'fullscreen'" class="fullscreen" @click="toggleScreen" />
  </div>
</template>

<script>
export default {
  name: 'ScreenFull',
  data() {
    return {
      isFullScreen: false // false 未开启  true已开启
    }
  },
  mounted() {
    document.addEventListener('fullscreenchange', (e) => {
      // 判断退出全屏的条件
      const isFull = document.fullscreenElement
      // isFull在退出全屏的时候 是null
      if (isFull == null) {
        console.log('退出全屏了')
        this.isFullScreen = false
      }
    })
  },
  methods: {
    toggleScreen() {
      // 切换全屏
      // 如果当前未开启 点击就开启  如果已开启 点击关闭全屏
      if (this.isFullScreen) {
        // 关闭
        document.exitFullscreen()
      } else {
        // 打开
        document.documentElement.requestFullscreen()
      }
      this.isFullScreen = !this.isFullScreen
    }
  }
}
</script>

<style lang="scss" scoped>
.screenWrapper{
  display: flex;
  align-items: center;
  margin-right: 20px;
}
.fullscreen {
  width: 20px;
  height: 20px;
  color: #fff;
  cursor: pointer;
}
</style>
